﻿using System.Windows;
using System.Windows.Controls;
using SmartSpace.UI.Silverlight;
using SmartSpace.UI.Silverlight.Theme;
using System.Windows.Media;
using System;

namespace SmartSpace.UI.Silverlight.Themes.ScienceFiction
{
    public partial class WorkAreaPanel : SlView, IWorkAreaPanel, ISlViewContainerObserver
    {
        public WorkAreaPanel()
        {
            InitializeComponent();
        }

        public void Init()
        {
            m_viewContainer.SwitchEffect = new FadeSwitchEffect();
            m_viewContainer.Attach(this);

            if ((Application.Current as SlApp).Params.DebugMode)
            {
                m_hyperlinkButtonDebug.Visibility = System.Windows.Visibility.Visible;
            }
        }

        #region IWorkAreaPanel Members

        public void Switch(SlView view)
        {
            m_viewContainer.SwitchView(view);
        }

        public event Action<SlView, int> SwitchExEvent;

        public void SwitchEx(SlView view, object arguments)
        {
            if (SwitchExEvent != null)
            {
                SwitchExEvent(view, (int)arguments);
            }
        }

        public SlView GetContentView()
        {
            return this;
        }

        #endregion

        #region ISlViewContainerObserver Members

        public void Update(SlViewContainer container)
        {
            m_navigationBar.Children.Clear();
            SlHeaderText site = new SlHeaderText();
            site.Content = "当前位置:";
            m_navigationBar.Children.Add(site);

            foreach (SlView view in container.Views)
            {
                if (m_navigationBar.Children.Count > 1)
                {
                    TextBlock txt = new TextBlock();
                    txt.FontSize = 15;
                    txt.FontWeight = FontWeights.Normal;
                    txt.Text = ">";
                    m_navigationBar.Children.Add(txt);
                }

                TextBlock btnText = new TextBlock();
                btnText.FontSize = 15;
                btnText.FontWeight = FontWeights.Normal;
                btnText.Foreground = site.Foreground;
                btnText.Text = view.GetCaption();
                HyperlinkButton btn = new HyperlinkButton();
                btn.BorderThickness = new Thickness(0, 0, 0, 0);
                btn.IsTabStop = false;
                btn.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                btn.Content = btnText;
            
                btn.Tag = view;
                btn.Click += new RoutedEventHandler(btn_Click);
                m_navigationBar.Children.Add(btn);
            }
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;
            SlView view = btn.Tag as SlView;
            if (view == m_viewContainer.CurrentView)
            {
                return;
            }

            m_viewContainer.GoBack(view);
        }

        #endregion

        private void m_hyperlinkButtonDebug_Click(object sender, RoutedEventArgs e)
        {
            SlView view = m_viewContainer.CurrentView;
            if (view != null)
            {
                MessageBox.Show(view.GetType().FullName);
            }
        }
    }
}
